/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package player;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
 * 
 * Parser XML
 * Matthieu PERRIN
 * Lit un fichier XML
 * Recupere l'ID, le nom d'un media
 * et l'IP du service de gestion des relay (manager)
 * 
 */
public class xml {
    
    String fichier = "";
    


    String id = "";
    String name = "";
    String manager = "";
    
    xml(String chemin)
    {
        fichier = chemin;
        parse();
    }
    
    private void parse()
    {
        try {
            File file = new File(fichier);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = (Document) db.parse(file);
            doc.getDocumentElement().normalize();
            NodeList nodeLst = doc.getElementsByTagName("media");

            
            
            for (int s = 0; s < nodeLst.getLength(); s++) {

                
                Node fstNode = nodeLst.item(s);
                
                

                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element Elmnt = (Element) fstNode;
                    
                    NodeList idElmntLst = Elmnt.getElementsByTagName("id");
                    Element idElmnt = (Element) idElmntLst.item(0);
                    NodeList fstNm = idElmnt.getChildNodes();
                    id = ((Node) fstNm.item(0)).getNodeValue();
                    
                    NodeList NmElmntLst = Elmnt.getElementsByTagName("name");
                    Element NmElmnt = (Element) NmElmntLst.item(0);
                    NodeList Nm = NmElmnt.getChildNodes();
                    name = ((Node) Nm.item(0)).getNodeValue();
                    
                    NodeList managerElmntLst = Elmnt.getElementsByTagName("manager");
                    Element managerElmnt = (Element) managerElmntLst.item(0);
                    NodeList lstNm2 = managerElmnt.getChildNodes();
                    manager = ((Node) lstNm2.item(0)).getNodeValue();
                }
                

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public String toString()
    {
        String s = "ID=" + id + " name=" + name + " Relay manager=" + manager;
        return s;
    }
    
    
    
    public String getId() {
        return id;
    }

    public String getManager() {
        return manager;
    }

    public String getName() {
        return name;
    }
}
